import { message } from 'antd';
import {
  checkCreateChargeBillTask,
  getChargeProgress,
  getList,
  deleteBill,
  getNsListAccuAndIns,
  getNsListOnce,
  getNsListWage,
  createBill,
  createNoEnterBill,
} from '@/services/chargemanage';

export default {
  state: {
    activeKey: '1',
    formFieldsValue: {},
    expandForm: false,
    isHasTask: false,
    billTaskId: null,
    taskCode: null,
    progressPercent: 0,
    pageData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    noBillData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    onceBillData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    wageGenerationData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
  },
  effects: {
    *getList({ payload }, { call, put }) {
      const res = yield call(getList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      if (res.success) {
        yield put({
          type: 'updateList',
          payload: {
            data: res.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(res.message);
      }
    },
    // 判断有无账单任务
    *checkCreateBillTask(_, { call, put }) {
      const response = yield call(checkCreateChargeBillTask);
      if (response.success && response.data) {
        if (response.data.taskCode === 3) {
          yield put({
            type: 'updateModelData',
            payload: {
              isHasTask: true,
              billTaskId: response.data.billTaskId,
              taskCode: response.data.taskCode,
            },
          });
        } else {
          yield put({
            type: 'updateModelData',
            payload: {
              isHasTask: false,
            },
          });
        }
      }
    },
    // 获取进度数据
    *getProgress({ payload }, { call, put }) {
      const response = yield call(getChargeProgress, payload);
      if (response.success) {
        yield put({
          type: 'updateModelData',
          payload: { progressPercent: response.data.rate },
        });
      }
      return response;
    },
    // 删除账单
    *deleteBill({ payload }, { call }) {
      const response = yield call(deleteBill, payload);
      return response;
    },
    // 其他列表数据 接口
    *getNsListAccuAndIns({ payload }, { call, put }) {
      const response = yield call(getNsListAccuAndIns, payload);
      yield put({
        type: 'getNoBillEnteredDataList',
        payload: {
          data: [],
          currentPage: 1,
          pageSize: 10,
        },
      });
      if (response.success) {
        const {
          pageArg: { currentPage, pageSize },
        } = payload;
        yield put({
          type: 'getNoBillEnteredDataList',
          payload: {
            data: response.data || [],
            currentPage,
            pageSize,
          },
        });
      } else {
        yield put({
          type: 'getNoBillEnteredDataList',
          payload: {
            data: [],
            currentPage: 1,
            pageSize: 10,
          },
        });
        message.warning(response.message || '获取列表失败');
      }
    },
    // 一次性收费数据 接口
    *getNsListOnce({ payload }, { call, put }) {
      const response = yield call(getNsListOnce, payload);
      yield put({
        type: 'getOnceDataList',
        payload: {
          data: [],
          currentPage: 1,
          pageSize: 10,
        },
      });
      if (response.success) {
        const {
          pageArg: { currentPage, pageSize },
        } = payload;
        yield put({
          type: 'getOnceDataList',
          payload: {
            data: response.data || [],
            currentPage,
            pageSize,
          },
        });
      } else {
        yield put({
          type: 'getOnceDataList',
          payload: {
            data: [],
            currentPage: 1,
            pageSize: 10,
          },
        });
        message.warning(response.message || '获取列表失败');
      }
    },
    // 代发工资待收费数据 接口
    *getNsListWage({ payload }, { call, put }) {
      const response = yield call(getNsListWage, payload);
      yield put({
        type: 'getWageGenerationDataList',
        payload: {
          data: [],
          currentPage: 1,
          pageSize: 10,
        },
      });
      if (response.success) {
        const {
          pageArg: { currentPage, pageSize },
        } = payload;
        yield put({
          type: 'getWageGenerationDataList',
          payload: {
            data: response.data || [],
            currentPage,
            pageSize,
          },
        });
      } else {
        yield put({
          type: 'getWageGenerationDataList',
          payload: {
            data: [],
            currentPage: 1,
            pageSize: 10,
          },
        });
        message.warning(response.message || '获取列表失败');
      }
    },
    // 代发工资生成账单
    *createBill({ payload }, { call }) {
      const response = yield call(createBill, payload);
      return response;
    },
    // 其他，一次性收费生成账单
    *createNoEnterBill({ payload }, { call }) {
      const response = yield call(createNoEnterBill, payload);
      return response;
    },
  },

  reducers: {
    updateList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 未进账单其它数据
    getNoBillEnteredDataList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list =
        data && data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        noBillData: {
          list,
          pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },

    // 未进账单一次性数据
    getOnceDataList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list =
        data && data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        onceBillData: {
          list,
          pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },

    // 代发工资代报税
    getWageGenerationDataList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list =
        data && data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        wageGenerationData: {
          list,
          pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },
  },
};
